<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="类目名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="类目名称"></el-input>
    </el-form-item>
    <el-form-item label="类目介绍" prop="description">
      <el-input v-model="dataForm.description" placeholder="类目介绍"></el-input>
    </el-form-item>
      <el-form-item label="首页图片" prop="picUriconUrll">
        <el-input v-model="dataForm.iconUrl" placeholder="类目首页图片"></el-input>
      </el-form-item>
    <el-form-item label="类目图片" prop="picUrl">
      <el-input v-model="dataForm.picUrl" placeholder="类目图片"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="sortOrder">
      <el-input v-model="dataForm.sortOrder" placeholder="排序"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          keywords: '',
          description: '',
          pid: '',
          iconUrl: '',
          picUrl: '',
          level: '',
          sortOrder: '',
          addTime: '',
          updateTime: '',
          deleted: ''
        },
        dataRule: {
          name: [
            { required: true, message: '类目名称不能为空', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '类目介绍不能为空', trigger: 'blur' }
          ],
          iconUrl: [
            { required: true, message: '类目首页图片不能为空', trigger: 'blur' }
          ],
          picUrl: [
            { required: true, message: '类目图片不能为空', trigger: 'blur' }
          ],
          sortOrder: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/product/procategory/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.procategory.name
                this.dataForm.keywords = data.procategory.keywords
                this.dataForm.description = data.procategory.description
                this.dataForm.pid = data.procategory.pid
                this.dataForm.iconUrl = data.procategory.iconUrl
                this.dataForm.picUrl = data.procategory.picUrl
                this.dataForm.level = data.procategory.level
                this.dataForm.sortOrder = data.procategory.sortOrder
                this.dataForm.addTime = data.procategory.addTime
                this.dataForm.updateTime = data.procategory.updateTime
                this.dataForm.deleted = data.procategory.deleted
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/product/procategory/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'keywords': this.dataForm.keywords,
                'description': this.dataForm.description,
                'pid': this.dataForm.pid,
                'iconUrl': this.dataForm.iconUrl,
                'picUrl': this.dataForm.picUrl,
                'level': this.dataForm.level,
                'sortOrder': this.dataForm.sortOrder,
                'addTime': this.dataForm.addTime,
                'updateTime': this.dataForm.updateTime,
                'deleted': this.dataForm.deleted
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
